-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
optimize splay tree #907
base: main
Are you sure you want to change the base?
optimize splay tree #907
Conversation
To prevent the tree from becoming skewed, I balanced it by splaying the first node of the sequence every 500 linear insert operations. The value 500 was determined experimentally.
WalkthroughThe changes in this pull request modify the Changes
Assessment against linked issues
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Outside diff range and nitpick comments (2)
packages/sdk/src/util/splay_tree.ts (2)
173-174
: Consider initializingfirstNode
in the constructorWhile
firstNode
is optional, explicitly initializing it in the constructor enhances code clarity and prevents potential undefined issues.Apply this diff to initialize
firstNode
:constructor(root?: SplayNode<V>) { this.root = root; this.linearCount = 0; + this.firstNode = undefined; }
263-274
: Add comments to clarify the splaying logic for linear insertionsAdding descriptive comments helps others understand how
linearCount
andfirstNode
work together to prevent tree skewness due to sequential insertions.Consider adding comments like this:
if (target === this.root) { + // Increment linearCount when inserting after the root node this.linearCount++; if (this.linearCount == 1) { + // Store the first node of the linear insertion sequence this.firstNode = newNode; } else if (this.linearCount > 500) { + // Splay the first node to rebalance the tree after 500 insertions this.splayNode(this.firstNode); this.linearCount = 0; } } else { + // Reset linearCount if insertion is not after the root this.linearCount = 0; }
this.splayNode(this.firstNode); | ||
this.linearCount = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure firstNode
is defined before splaying
Although firstNode
should be defined when linearCount > 500
, adding a safety check can prevent unexpected errors if the state changes in the future.
Apply this diff to add a null check:
} else if (this.linearCount > 500) {
+ if (this.firstNode) {
this.splayNode(this.firstNode);
+ }
this.linearCount = 0;
Committable suggestion was skipped due to low confidence.
packages/sdk/src/util/splay_tree.ts
Outdated
if (target == this.root) { | ||
this.linearCount++; | ||
if (this.linearCount==1) { | ||
this.firstNode = newNode; | ||
} else if (this.linearCount > 500) { | ||
this.splayNode(this.firstNode); | ||
this.linearCount = 0; | ||
} | ||
} else { | ||
this.linearCount = 0; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Make the splay threshold configurable
Hardcoding the value 500
limits flexibility. Defining it as a constant or making it configurable allows for easier tuning based on different use cases.
Define a constant at the top of the class:
export class SplayTree<V> {
private root?: SplayNode<V>;
+ private static readonly SPLAY_THRESHOLD = 500;
private linearCount: number;
private firstNode?: SplayNode<V>;
Update the code to use the constant:
- } else if (this.linearCount > 500) {
+ } else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {
Committable suggestion was skipped due to low confidence.
packages/sdk/src/util/splay_tree.ts
Outdated
if (target == this.root) { | ||
this.linearCount++; | ||
if (this.linearCount==1) { | ||
this.firstNode = newNode; | ||
} else if (this.linearCount > 500) { | ||
this.splayNode(this.firstNode); | ||
this.linearCount = 0; | ||
} | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use strict equality ===
instead of ==
Using strict equality ensures both value and type are compared, preventing potential bugs with type coercion.
Apply this diff to use strict equality:
-if (target == this.root) {
+if (target === this.root) {
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (target == this.root) { | |
this.linearCount++; | |
if (this.linearCount==1) { | |
this.firstNode = newNode; | |
} else if (this.linearCount > 500) { | |
this.splayNode(this.firstNode); | |
this.linearCount = 0; | |
} | |
} else { | |
if (target === this.root) { | |
this.linearCount++; | |
if (this.linearCount==1) { | |
this.firstNode = newNode; | |
} else if (this.linearCount > 500) { | |
this.splayNode(this.firstNode); | |
this.linearCount = 0; | |
} | |
} else { |
Move hardcoded constant to readonly attribute in Splay Tree
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
packages/sdk/src/util/splay_tree.ts (1)
173-175
: LGTM! Consider making SPLAY_THRESHOLD configurable.The new properties align well with the PR objectives for optimizing the splay tree performance. The static
SPLAY_THRESHOLD
effectively implements the proposed solution of splaying every 500 linear insert operations.To enhance flexibility, consider making
SPLAY_THRESHOLD
configurable:- private static readonly SPLAY_THRESHOLD = 500; + private static SPLAY_THRESHOLD = 500; + + public static setSplayThreshold(threshold: number): void { + if (threshold > 0) { + SplayTree.SPLAY_THRESHOLD = threshold; + } + }This allows for easier tuning based on different use cases or performance requirements.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- packages/sdk/src/util/splay_tree.ts (4 hunks)
🔇 Additional comments (2)
packages/sdk/src/util/splay_tree.ts (2)
179-179
: LGTM! Proper initialization of linearCount.Initializing
linearCount
to 0 in the constructor is correct and aligns with the new balancing strategy. This ensures that each new SplayTree instance starts with a fresh count for linear insertions.
218-218
: Consider the impact of splaying after find operations.While splaying after find operations can improve amortized performance in typical splay trees, it might interfere with the new balancing strategy for linear insertions. Splaying nodes found by the
find
method could potentially break linear sequences that the new strategy is trying to manage.To assess the impact, let's analyze the usage of the
find
method:Consider whether splaying after find operations is necessary given the new balancing strategy, and evaluate its potential impact on the overall performance objectives of this PR.
if (target == this.root) { | ||
this.linearCount++; | ||
if (this.linearCount == 1) { | ||
this.firstNode = newNode; | ||
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) { | ||
this.splayNode(this.firstNode); | ||
this.linearCount = 0; | ||
} | ||
} else { | ||
this.linearCount = 0; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implement the new balancing strategy with some improvements.
The new logic effectively implements the proposed balancing strategy for linear insertions, aligning well with the PR objectives. However, there are a few points to address:
- Use strict equality (
===
) instead of loose equality (==
) for comparisons. - Add a null check before splaying
firstNode
. - Consider resetting
firstNode
after splaying.
Apply this diff to address the points mentioned:
- if (target == this.root) {
+ if (target === this.root) {
this.linearCount++;
- if (this.linearCount == 1) {
+ if (this.linearCount === 1) {
this.firstNode = newNode;
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {
- this.splayNode(this.firstNode);
+ if (this.firstNode) {
+ this.splayNode(this.firstNode);
+ this.firstNode = undefined;
+ }
this.linearCount = 0;
}
} else {
this.linearCount = 0;
+ this.firstNode = undefined;
}
These changes improve type safety, prevent potential null pointer exceptions, and ensure proper reset of the firstNode
reference.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (target == this.root) { | |
this.linearCount++; | |
if (this.linearCount == 1) { | |
this.firstNode = newNode; | |
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) { | |
this.splayNode(this.firstNode); | |
this.linearCount = 0; | |
} | |
} else { | |
this.linearCount = 0; | |
} | |
if (target === this.root) { | |
this.linearCount++; | |
if (this.linearCount === 1) { | |
this.firstNode = newNode; | |
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) { | |
if (this.firstNode) { | |
this.splayNode(this.firstNode); | |
this.firstNode = undefined; | |
} | |
this.linearCount = 0; | |
} | |
} else { | |
this.linearCount = 0; | |
this.firstNode = undefined; | |
} |
To prevent the tree from becoming skewed, I balanced it by splaying the first node of the sequence every 500 linear insert operations. The value 500 was determined experimentally.
What this PR does / why we need it:
Description:
This pull request (PR) introduces an enhanced method for balancing a tree during insert operations to prevent skewness. The goal is to optimize performance by replacing the inefficient height-based splay operations with a more effective approach.
Changes Made:
Linear Operation Detection and Balancing:
Optimization of Splay Trigger Frequency:
Code Modifications:
linearCount
andfirstNode
fields to theTree
structure to track consecutive insert operations.InsertAfter
method to conditionally perform a splay operation and resetlinearCount
.If constant 500 change to 50 , then splay tree looks like this.
Performance Improvements:
For more details, you can check this
Which issue(s) this PR fixes:
Fixes #941
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
Additional documentation:
Checklist:
Summary by CodeRabbit
Summary by CodeRabbit